<!--
 * @Author: 陈心怡
 * @Date: 2024-09-20 14:29:47
 * @LastEditors: 陈心怡
 * @LastEditTime: 2024-09-20 15:52:36
 * @FilePath: \vue3-web-study\src\views\news\components\news-list.vue
 * @Description: 
-->
<template>
  <div class="news-container w1200">
    <ul class="news-list flex flex-wrap">
      <li v-for="item in newsList" :key="item._id">
        <router-link :to="`/detail/${item._id}`">
          <img :src="item.cover" width="390" height="220" alt="new" />
          <h3 class="ellipsis">{{ item.title }}</h3>
          <div class="date">{{ item.updatedAt }}</div>
        </router-link>
      </li>
    </ul>
    <!-- 分页 -->
    <el-pagination
      background
      size="small"
      layout="total, sizes, prev, pager, next, jumper"
      v-model:current-page="paginationConfig.pageNumber"
      v-model:page-size="paginationConfig.pageSize"
      :total="paginationConfig.total"
      :page-sizes="[1, 2, 3]"
      style="justify-content: center"
      @size-change="handelChangeSize"
      @current-change="handelChangeCurrent"
    />
  </div>
</template>

<script setup>
defineOptions({
  name: 'NewsList'
})
import { ref } from 'vue'
import { getNewsList } from '@/api/news'
import { useRouter, onBeforeRouteUpdate } from 'vue-router'

const router = useRouter()
const newsList = ref([])
const paginationConfig = ref({
  total: 0,
  pageSize: 2,
  pageNumber: 1
})

// 调取接口获取新闻列表数据
const res = async () => {
  const { data, pagination } = await getNewsList({
    pageNumber: paginationConfig.value.pageNumber,
    pageSize: paginationConfig.value.pageSize
  })
  newsList.value = data
  paginationConfig.value = pagination
}

// 选择页码
const handelChangeCurrent = (currentPage) => {
  paginationConfig.value.pageNumber = currentPage
  router.push({
    path: '/news',
    query: { pageNumber: currentPage, pageSize: paginationConfig.value.pageSize }
  })
}

// 选择每页显示条数
const handelChangeSize = (pageSize) => {
  paginationConfig.value.pageSize = pageSize
  router.push({
    path: '/news',
    query: { pageNumber: paginationConfig.value.pageNumber, pageSize: pageSize }
  })
}
// 初始化数据
res()

// 监听路由变化
onBeforeRouteUpdate((to) => {
  const pageSize = paginationConfig.value.pageSize
  paginationConfig.value.pageNumber = parseInt(to.query.pageNumber) || 1
  paginationConfig.value.pageSize = parseInt(to.query.pageSize) || pageSize
  res()
})
</script>

<style lang="scss" scoped>
.news-container {
  padding: 80px 0 50px;
  :deep(.el-pagination.is-background .el-pager li) {
    border-radius: 50%;
    width: 35px;
    height: 35px;
    line-height: 35px;
    &:hover {
      background-color: #ff7200;
      color: #ffffff;
    }
  }
  :deep(.el-pagination) {
    line-height: 35px;
    & > * {
      vertical-align: middle;
    }
    .btn-prev,
    .btn-next {
      border-radius: 50%;
      width: 35px;
      height: 35px;
    }
  }
}
.news-list {
  min-height: 325px;
  padding-bottom: 90px;
  li {
    margin: 0 20px 70px 0;
    width: calc((100% - 40px) / 3);
    &:nth-child(3n) {
      margin-right: 0;
    }
    &:hover {
      h3 {
        color: #ff7200;
      }
    }
    img {
      width: 390px;
      height: 220px;
    }
    h3 {
      font-size: 16px;
      margin-top: 20px;
      padding-left: 20px;
      width: 390px;
      color: #333;
    }
    .date {
      padding-left: 20px;
      margin-top: 30px;
      font-size: 14px;
      color: #666;
    }
  }
}
</style>
